package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.BusinessDto;
import com.qk.entity.Business;
import com.qk.service.BusinessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/businesses")
public class BusinessController {
    @Autowired
    private BusinessService businessService;

    //新增商机 (用的Mybatis-plus省事儿多了,简单的单表增删改查,不用写了)
    @PostMapping
    public Result addBusiness(@RequestBody Business business) {
        log.info("新增商机");
        business.setStatus(1);
        business.setCreateTime(LocalDateTime.now());
        business.setUpdateTime(LocalDateTime.now());
        businessService.save(business);
        return Result.success();
    }

    //商机列表查询
    @GetMapping
    public Result list(BusinessDto businessDto) {
        log.info("商机列表查询");
        PageResult<Business> pageResult = businessService.listBusinesses(businessDto);
        return Result.success(pageResult);
    }

    //3. 分配商机
    @PutMapping("/assign/{businessId}/{userId}")
    public Result assign(@PathVariable Integer businessId, @PathVariable Integer userId) {
        log.info("分配商机 :商机ID={},用户ID={} ", businessId, userId);
        Business business = new Business();
        business.setId(businessId);
        business.setUserId(userId);
        business.setStatus(2);//商机状态，1:待分配，2:待跟进，3:跟进中，4:回收，5：转客户
        business.setUpdateTime(LocalDateTime.now());
        businessService.updateById(business);
        return Result.success();
    }
    //商机跟进主要分为两步:1.查询回显(这个又包括两部分信息: a.商机基本信息 b.商机跟进记录列表)
    //                 2.商机跟进
      //根据ID查询商机
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("查询商机信息  " + id);
        Business business = businessService.getBusinessById(id);
        return Result.success(business);
    }
    // 跟进商机(在跟进商机的时候,点击确定按钮时,会执行两步操作:1.根据ID更新商机基本信息 2.添加商机跟进记录)
    @PutMapping
    public Result trackBusiness(@RequestBody Business business) {
        log.info("跟进商机信息  " + business);
        businessService.trackBusiness(business);
        return Result.success();
    }

    //踢回公海(踢回公海，其实本质就是修改商机的状态，将该商机的状态修改为 4 )
    @PutMapping("/back/{id}")
    public Result back(@PathVariable Integer id) {
        log.info("踢回公海  " + id);
        Business business = new Business();
        business.setId(id);
        business.setStatus(4);//商机状态，1:待分配，2:待跟进，3:跟进中，4:回收，5：转客户
        business.setUpdateTime(LocalDateTime.now());
        businessService.updateById(business);
        return Result.success();
    }

    //转客户处理
    @PostMapping("/toCustomer/{id}")
    public Result toCustomer(@PathVariable Integer id) {
        log.info("转客户处理  " + id);
        businessService.toCustomer(id);
        return Result.success();
    }

    //公海池列表查询
    @GetMapping("/pool")
    public Result pool(BusinessDto businessDto) {
        log.info("公海池列表查询  " + businessDto);
        PageResult<Business> pageResult = businessService.listpoolBusinesses(businessDto);
        return Result.success(pageResult);
    }

}
